<template>
  <div class="search-result-item" @click="$emit('click')">
    <a-card class="item-card" :class="{ 'hover-effect': true }">
      <div class="card-content flex">
        <!-- 图片部分 -->
        <div v-if="item.image" class="item-image">
          <img
            :src="item.image"
            :alt="item.title"
            class="object-cover w-full h-full rounded-md"
          />
          <!-- 类型标签 -->
          <div class="item-type-badge">
            <a-tag :color="getTypeColor(item.type)" size="small">
              {{ getTypeLabel(item.type) }}
            </a-tag>
          </div>
        </div>

        <!-- 内容部分 -->
        <div class="item-body flex-1 flex flex-col">
          <div class="item-header">
            <h3 class="item-title">
              {{ item.title }}
              <a-tooltip content="在新标签页中打开" position="top">
                <icon-link class="ml-2 text-gray-400 dark:text-gray-600" />
              </a-tooltip>
            </h3>
            <div class="item-meta">
              <!-- 评分 -->
              <div v-if="item.rating" class="meta-rating">
                <a-rate
                  :model-value="parseFloat(item.rating)"
                  allow-half
                  readonly
                  :count="5"
                  size="small"
                />
                <span class="rating-text">{{ item.rating }}</span>
              </div>
              <!-- 创建时间 -->
              <div class="meta-time">
                <icon-clock-circle class="meta-icon" />
                <span>{{ formatTime(item.createdAt) }}</span>
              </div>
              <!-- 浏览次数 -->
              <div class="meta-views">
                <icon-eye class="meta-icon" />
                <span>{{ formatNumber(item.viewCount) }}</span>
              </div>
            </div>
          </div>

          <div class="item-description">
            {{ item.description }}
          </div>

          <!-- 标签和操作 -->
          <div class="item-footer">
            <div class="item-tags">
              <a-space>
                <a-tag
                  v-for="(tag, i) in item.tags"
                  :key="i"
                  size="small"
                  bordered
                >
                  {{ tag }}
                </a-tag>
              </a-space>
            </div>
            <div class="item-actions">
              <a-space size="mini">
                <a-button type="text" size="mini">
                  <template #icon><icon-share /></template>
                  分享
                </a-button>
                <a-button type="text" size="mini">
                  <template #icon><icon-star /></template>
                  收藏
                </a-button>
                <a-button type="text" size="mini" v-if="item.type === 'spot'">
                  <template #icon><icon-location /></template>
                  导航
                </a-button>
              </a-space>
            </div>
          </div>
        </div>
      </div>
    </a-card>
  </div>
</template>

<script setup>
import {
  IconLink,
  IconStar,
  IconShare,
  IconClockCircle,
  IconEye,
  IconLocation,
} from "@arco-design/web-vue/es/icon";

defineEmits(["click"]);

const props = defineProps({
  item: {
    type: Object,
    required: true,
  },
});

// 获取类型颜色
const getTypeColor = (type) => {
  switch (type) {
    case "spot":
      return "blue";
    case "guide":
      return "green";
    case "product":
      return "orange";
    case "news":
      return "purple";
    default:
      return "gray";
  }
};

// 获取类型标签文字
const getTypeLabel = (type) => {
  switch (type) {
    case "spot":
      return "景点";
    case "guide":
      return "攻略";
    case "product":
      return "商品";
    case "news":
      return "资讯";
    default:
      return "其他";
  }
};

// 格式化时间
const formatTime = (timestamp) => {
  if (!timestamp) return "";

  const date = new Date(timestamp);
  const now = new Date();
  const diff = now - date;

  // 小于一天显示几小时前
  if (diff < 86400000) {
    const hours = Math.floor(diff / 3600000);
    return hours <= 0 ? "刚刚" : `${hours}小时前`;
  }

  // 小于7天显示几天前
  if (diff < 604800000) {
    const days = Math.floor(diff / 86400000);
    return `${days}天前`;
  }

  // 否则显示具体日期
  return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, "0")}-${String(date.getDate()).padStart(2, "0")}`;
};

// 格式化数字
const formatNumber = (num) => {
  if (!num && num !== 0) return "";

  if (num < 1000) return num.toString();
  if (num < 10000) return (num / 1000).toFixed(1) + "K";
  return (num / 10000).toFixed(1) + "万";
};
</script>

<style lang="scss" scoped>
.search-result-item {
  @apply w-full cursor-pointer;

  .item-card {
    @apply bg-white dark:bg-gray-800 transition-all duration-300;

    &.hover-effect {
      &:hover {
        @apply shadow-md transform scale-[1.005];
      }
    }

    :deep(.arco-card-body) {
      @apply p-0;
    }
  }

  .card-content {
    @apply p-0;
  }

  .item-image {
    @apply relative w-32 md:w-40 lg:w-48 h-28 md:h-32 overflow-hidden rounded-l-lg shrink-0;

    img {
      @apply transition-transform duration-300;

      .hover-effect:hover & {
        @apply transform scale-110;
      }
    }

    .item-type-badge {
      @apply absolute top-2 left-2;
    }
  }

  .item-body {
    @apply p-4 md:p-5;
  }

  .item-header {
    @apply mb-2;

    .item-title {
      @apply text-lg font-medium text-gray-900 dark:text-white mb-2 flex items-center;

      .arco-icon {
        @apply opacity-0 transition-opacity duration-200;

        .hover-effect:hover & {
          @apply opacity-100;
        }
      }
    }
  }

  .item-meta {
    @apply flex flex-wrap items-center text-xs text-gray-500 dark:text-gray-400 gap-3 mb-2;

    .meta-rating {
      @apply flex items-center;

      .rating-text {
        @apply ml-1;
      }
    }

    .meta-time,
    .meta-views {
      @apply flex items-center;

      .meta-icon {
        @apply mr-1;
      }
    }
  }

  .item-description {
    @apply text-sm text-gray-600 dark:text-gray-300 line-clamp-2 flex-grow mb-3;
  }

  .item-footer {
    @apply flex justify-between items-center mt-auto pt-2 border-t border-gray-100 dark:border-gray-700;

    .item-tags {
      @apply flex-1 overflow-hidden;

      :deep(.arco-tag) {
        @apply text-xs;
      }
    }

    .item-actions {
      @apply ml-2 flex-shrink-0;

      :deep(.arco-btn) {
        @apply text-xs text-gray-500 dark:text-gray-400;

        &:hover {
          @apply text-primary-500 dark:text-primary-400;
        }
      }
    }
  }
}

@media (max-width: 639px) {
  .search-result-item {
    .card-content {
      @apply flex-col;
    }

    .item-image {
      @apply w-full h-40 rounded-t-lg rounded-l-none;
    }

    .item-footer {
      @apply flex-col items-start gap-2;

      .item-actions {
        @apply ml-0 w-full justify-end;
      }
    }
  }
}
</style>
